#include "updateinformation.h"
#include "ui_updateinformation.h"
#include "mainwidget.h"
#include "ui_mainwidget.h"
#include <QFileDialog>
#include <QDebug>

updateInformation::updateInformation(QWidget *parent, Person * m_user) :
    QWidget(parent),
    ui(new Ui::updateInformation),
    update_user(m_user)
{
    ui->setupUi(this);
    ui->labPixmap->setScaledContents(true);
    ui->editName->setMaxLength(7);
    ui->editPhoneNumber->setMaxLength(11);
//    QIntValidator * validator = new QIntValidator(0, 100000000000);
//    ui->editPhoneNumber->setValidator(validator);

    updateInfo();
    connectSettings();
}

updateInformation::~updateInformation()
{
    delete ui;
}

void updateInformation::updateInfo()
{
    ui->editName->setText(update_user->getName());
    ui->editPhoneNumber->setText(update_user->getPhoneNumber());
    ui->editSignature->setText(update_user->getSignature());
    editSignature_textChanged();
    if(bool(update_user->getSex()))
    {
        ui->rbtnMan->setChecked(true);
        ui->rbtnWoman->setChecked(false);
        sex = true;
    }
    else
    {
        ui->rbtnMan->setChecked(false);
        ui->rbtnWoman->setChecked(true);
        sex = false;
    }
    ui->editBirthday->setDate(update_user->getBirthday());
    ui->labPixmap->setPixmap(update_user->getPixmapPath());
    pixmapPath = update_user->getPixmapPath();
}

void updateInformation::connectSettings()
{
    connect(ui->btnSave, &QPushButton::clicked, this, &updateInformation::btnSave_clicked);
    connect(ui->btnCancel, &QPushButton::clicked, this, &updateInformation::btnCancel_clicked);
    connect(ui->btnPixmap, &QPushButton::clicked, this, &updateInformation::btnPixmap_clicked);
    connect(ui->btnPassword, &QPushButton::clicked, this, &updateInformation::btnPassword_clicked);
    connect(ui->rbtnMan, &QPushButton::clicked, [=](){sex = true;});
    connect(ui->rbtnWoman, &QPushButton::clicked, [=](){sex = false;});

    //个签字数限制
    connect(ui->editSignature, &QTextEdit::textChanged, this, &updateInformation::editSignature_textChanged);
//    //电话号码限制
//    connect(ui->editPhoneNumber, &QLineEdit::textChanged, this, &updateInformation::editPhoneNumber_textChanged);
}

void updateInformation::btnSave_clicked()
{
    //数据保存
    update_user->setName(ui->editName->text());
    update_user->setPhoneNumber(ui->editPhoneNumber->text());
    update_user->setSignature(ui->editSignature->toPlainText());
    if(sex)
    {
        update_user->setSex(1);
    }
    else
    {
        update_user->setSex(0);
    }
    update_user->setBirthday(ui->editBirthday->date());
    update_user->setPixmap(* ui->labPixmap->pixmap());
    update_user->setPixmapPath(pixmapPath);

    emit sendData(update_user);

    //发送修改后的个人信息
    // URL
    QString baseUrl = "http://127.0.0.1:5000/UserInfoUpdate?Userid=" + update_user->getId() + "&Nickname=" + update_user->getName()
            + "&Signature=" + update_user->getSignature() + "&Sex=" + QString::number(update_user->getSex()) + "&Birthday=" + update_user->getBirthday().toString(Qt::ISODate)
            + "&Image=" + pixmapPath + "&Phone=" + update_user->getPhoneNumber();
    QUrl url(baseUrl);
    qDebug() << baseUrl;

    // 构造请求QNetworkRequest
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setUrl(url);

    QNetworkAccessManager *manager = new QNetworkAccessManager();

    // 发送get请求
    manager->get(request);
}

void updateInformation::btnCancel_clicked()
{
    updateInfo();

    emit restoreDefault();
}

void updateInformation::btnPixmap_clicked()
{
    QString imgPath = QFileDialog::getOpenFileName(this, "Open File", "E:\\Picture\\Ps", "Image File(*.jpg;*.png)");
    if(!imgPath.isNull())
    {
        pixmapPath = imgPath;
        ui->labPixmap->setPixmap(QPixmap(imgPath));
    }
}

void updateInformation::btnPassword_clicked()
{
    if(update_PassWord_Widget == nullptr)
    {
        update_PassWord_Widget = new updatePassWord(nullptr, update_user);
        //设置窗口为应用程序的模态
        update_PassWord_Widget->setWindowModality(Qt::ApplicationModal);
    }
    update_PassWord_Widget->setGeometry(574, 325, 679, 419);
    update_PassWord_Widget->raise();
    update_PassWord_Widget->show();
}

void updateInformation::editSignature_textChanged()
{
    QString textContent = ui->editSignature->toPlainText();

    int length = textContent.count();
    int maxLength = MAX_SIZE;

    if(length > maxLength)
    {
        //返回当前光标的副本
        QTextCursor cursor = ui->editSignature->textCursor();
        cursor.movePosition(QTextCursor::End);
        if(cursor.hasSelection())
        {
            cursor.clearSelection();
        }
        //删除前一个字符
        cursor.deletePreviousChar();
        //刷新光标位置
        ui->editSignature->setTextCursor(cursor);
    }

    length = ui->editSignature->toPlainText().count();
    ui->labSize->setText(QString("%1").arg(MAX_SIZE-length));
}

//void updateInformation::editPhoneNumber_textChanged()
//{
//    QString textContent = ui->editSignature->toPlainText();

//    int length = textContent.count();
//    int maxLength = MAX_SIZE;
//}
